package com.tongda.core.repository;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.tongda.core.common.Constants;
import com.tongda.core.utils.DBHelper;
import com.tongda.core.vo.DishesCategory;

public class DishCategoryDAO {
	private String TABLE_NAME = "dish_category";
	private DBHelper dbHelper;
	private SQLiteDatabase db;
	private static DishCategoryDAO instance;

	public DishCategoryDAO(Context context) {
		this.dbHelper = DBHelper.getInstance(context);
	}

	public DishCategoryDAO() {

	}

	public static synchronized DishCategoryDAO getInstance(Context context) {
		if (instance == null) {
			instance = new DishCategoryDAO(context);
		}
		return instance;
	}

	public void clearDishCategory() {
		try {
			db = dbHelper.openDatabase();
			String checkSql = "SELECT name FROM sqlite_master WHERE type='table' and name='"+TABLE_NAME+"'";
			Cursor cursor = db.rawQuery(checkSql, null);
			if (cursor.getCount() > 0) {
				// 调试阶段暂时都清空 因为表结构经常变化
				db.execSQL("drop table IF EXISTS "+TABLE_NAME);
				db.execSQL(dbHelper.DISH_CATEGORY_TABLE_CREATE);
			} else {
				db.execSQL(dbHelper.DISH_CATEGORY_TABLE_CREATE);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		dbHelper.closeDatabase();
	}

	/**
	 * 新增
	 */
	public void insert(DishesCategory dishesCategory) {
		synchronized (Constants.objectLock) {
			db = dbHelper.openDatabase();
			ContentValues values = new ContentValues();
			values.put("id", dishesCategory.getCategoryId());
			values.put("name", dishesCategory.getCategoryName());
			values.put("code", dishesCategory.getCategoryCode());
			db.insert(TABLE_NAME, null, values);
			dbHelper.closeDatabase();
		}
	}
	
	public void initInsert(List<DishesCategory> dishesCategorys) {
		synchronized (Constants.objectLock) {
			db = dbHelper.openDatabase();
			
			String checkSql = "SELECT name FROM sqlite_master WHERE type='table' and name='"+TABLE_NAME+"'";
			Cursor cursor = db.rawQuery(checkSql, null);
			if (cursor.getCount() > 0) {
				// 调试阶段暂时都清空 因为表结构经常变化
				db.execSQL("drop table IF EXISTS "+TABLE_NAME);
				db.execSQL(dbHelper.DISH_CATEGORY_TABLE_CREATE);
			} else {
				db.execSQL(dbHelper.DISH_CATEGORY_TABLE_CREATE);
			}
			
			for(DishesCategory dishesCategory:dishesCategorys){
				ContentValues values = new ContentValues();
				values.put("id", dishesCategory.getCategoryId());
				values.put("name", dishesCategory.getCategoryName());
				values.put("code", dishesCategory.getCategoryCode());
				values.put("categoryLevel", dishesCategory.getCategoryLevel());
				values.put("parentCategoryId", dishesCategory.getParentCategoryId());
				db.insert(TABLE_NAME, null, values);
			}
			dbHelper.closeDatabase();
		}
	}
	

	public void buildInsert(List<DishesCategory> dishesCategorys) {
		synchronized (Constants.objectLock) {
			db = dbHelper.openDatabase();
			for (DishesCategory dishesCategory : dishesCategorys) {
				ContentValues values = new ContentValues();
				values.put("id", dishesCategory.getCategoryId());
				values.put("name", dishesCategory.getCategoryName());
				values.put("code", dishesCategory.getCategoryCode());
				values.put("categoryLevel", dishesCategory.getCategoryLevel());
				values.put("parentCategoryId",
						dishesCategory.getParentCategoryId());
				db.insert(TABLE_NAME, null, values);
			}
			dbHelper.closeDatabase();
		}
	}

	public void insertNew(DishesCategory dishesCategory) {
		db = dbHelper.openDatabase();
		ContentValues values = new ContentValues();
		values.put("id", dishesCategory.getCategoryId());
		db.insert("orderdish_category", "count", values);
		dbHelper.closeDatabase();
	}

	public void updateCount(int count, String categoryId) {
		db = dbHelper.openDatabase();
		ContentValues values = new ContentValues();
		values.put("count", count);
		db.update("orderdish_category", values, "id = ?",
				new String[] { categoryId + "" });
		dbHelper.closeDatabase();
	}

	public DishesCategory getCategory(String categoryId) {
		DishesCategory category = new DishesCategory();
		try {
			db = dbHelper.getReadableDatabase();
			Cursor cursor = db.rawQuery(
					"select count from orderdish_category where id = ?",
					new String[] { categoryId + "" });
			while (cursor.moveToNext()) {
				category.setCategoryCount(cursor.getInt(cursor
						.getColumnIndex("count")));
			}
		} catch (Exception e) {
		}
		return category;
	}

	public ArrayList<DishesCategory> getAll() {
		ArrayList<DishesCategory> dishesCategorys = new ArrayList<DishesCategory>();
		try {
			db = dbHelper.getReadableDatabase();
			// Cursor cursor = db.rawQuery("select * from " + TABLE_NAME +
			// " where 1 = 1", new String[] { String.valueOf(0) });
			// Cursor cursor =
			// db.rawQuery("select dish_category. *   from dish_category left join orderdish_category  on dish_category.id=orderdish_category.id  order by orderdish_category.count desc ",
			// null);
			Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
			while (cursor.moveToNext()) {
				DishesCategory dishesCategory = new DishesCategory();
				dishesCategory.setCategoryName(cursor.getString(cursor
						.getColumnIndex("name")));
				dishesCategory.setCategoryCode(cursor.getString(cursor
						.getColumnIndex("code")));
				dishesCategory.setCategoryId(cursor.getString(cursor
						.getColumnIndex("id")));
				dishesCategory.setCategoryLevel(cursor.getString(cursor
						.getColumnIndex("categoryLevel")));
				dishesCategory.setParentCategoryId(cursor.getString(cursor
						.getColumnIndex("parentCategoryId")));
				dishesCategorys.add(dishesCategory);
			}
			cursor.close();
		} catch (Exception e) {
			Log.e(this.toString(), e.getMessage());
		}
		return dishesCategorys;
	}

	public List<DishesCategory> getsubDishesCategory(String categoryId) {
		List<DishesCategory> subDishesCategory = new ArrayList<DishesCategory>();
		db = dbHelper.getReadableDatabase();
		Cursor cursor = null;
		if (categoryId != null) {
			cursor = db
					.rawQuery(
							"select * from dish_category where categoryLevel ='2' and parentCategoryId = ?",
							new String[] { categoryId });
		} else {
			cursor = db.rawQuery("select * from dish_category", null);
		}

		while (cursor.moveToNext()) {
			DishesCategory dishesCategory = new DishesCategory();
			dishesCategory.setCategoryName(cursor.getString(cursor
					.getColumnIndex("name")));
			dishesCategory.setCategoryCode(cursor.getString(cursor
					.getColumnIndex("code")));
			dishesCategory.setCategoryId(cursor.getString(cursor
					.getColumnIndex("id")));
			dishesCategory.setCategoryLevel(cursor.getString(cursor
					.getColumnIndex("categoryLevel")));
			dishesCategory.setParentCategoryId(cursor.getString(cursor
					.getColumnIndex("parentCategoryId")));
			subDishesCategory.add(dishesCategory);
		}
		cursor.close();
		return subDishesCategory;
	}

	public ArrayList<DishesCategory> getDishesCategory() {
		ArrayList<DishesCategory> dishesCategorys = new ArrayList<DishesCategory>();
		try {
			db = dbHelper.getReadableDatabase();
			// Cursor cursor = db.rawQuery("select * from " + TABLE_NAME +
			// " where 1 = 1", new String[] { String.valueOf(0) });
			// Cursor cursor =
			// db.rawQuery("select dish_category. *   from dish_category left join orderdish_category  on dish_category.id=orderdish_category.id  order by orderdish_category.count desc ",
			// null);
			Cursor cursor = db
					.rawQuery(
							"select * from dish_category where categoryLevel in('1','')",
							null);
			while (cursor.moveToNext()) {
				DishesCategory dishesCategory = new DishesCategory();
				dishesCategory.setCategoryName(cursor.getString(cursor
						.getColumnIndex("name")));
				dishesCategory.setCategoryCode(cursor.getString(cursor
						.getColumnIndex("code")));
				dishesCategory.setCategoryId(cursor.getString(cursor
						.getColumnIndex("id")));
				dishesCategory.setCategoryLevel(cursor.getString(cursor
						.getColumnIndex("categoryLevel")));
				dishesCategory.setParentCategoryId(cursor.getString(cursor
						.getColumnIndex("parentCategoryId")));
				dishesCategorys.add(dishesCategory);
			}
			cursor.close();
		} catch (Exception e) {
			Log.e(this.toString(), e.getMessage());
		}
		return dishesCategorys;
	}
}
